      SUBROUTINE CWNS(FREQ,DEPB,SVPB,DBPWLB,cgwnsb,cwnsb,NPB,MX)
C
C     THIS SUBROUTINE GENERATES THE COEFFICIENTS cgwnsb AND cwnsb 
C     IN A PIECEWISE LINEAR REPRESENTATION OF THE SQUARE OF THE 
C     WAVE NUMBER.
C
C     CK(Z)**2 = cgwnsb(I)*Z + cwnsb(I), I=1,NPB
C
      IMPLICIT REAL*8 (A-H,O-Z)
C
      COMMON /BLKEVN/ HB,CW,CB,FKW,FKB,ROHW,ROHB,ATEN
C
      DIMENSION DEPB(MX),SVPB(MX),DBPWLB(MX)
      COMPLEX*16 cgwnsb(MX),cwnsb(MX)
      COMPLEX*16 CI,CK,CKP1,CONSQ,CONSQP1
C
      CI=DCMPLX(0.0D0,1.0D0)
      PI=2.0D0*DACOS(0.0D0)
C
C     CONVERSION FACTOR OF DB/WAVELENGTH TO IMAG PART OF 
C     THE COMPLEX WAVE NUMBER 
C
      ETA=1.0D0/(40.0D0*PI*DLOG10(DEXP(1.0D0)))
C
      NPBM1=NPB-1
C
C     FIND THE SQUARE OF THE WAVE NUMBER AND INDEX OF REFARCTION
C     AT THE DEPB(1)
C
      CK=(2.0D0*PI*FREQ/SVPB(1))*(1.0D0+CI*ETA*DBPWLB(1))
      CONSQ=CK**2
C
      DO 100 I=1,NPBM1
C
C     FIND THE SQUARE OF THE WAVE NUMBER AND INDEX OF REFARCTION
C     AT THE DEPB(I+1)
C
      CKP1=(2.0D0*PI*FREQ/SVPB(I+1))*(1.0D0+CI*ETA*DBPWLB(I+1))
      CONSQP1=CKP1**2
C
C     COMPUTE THE GRADIENT AND INTERCEPT
C
      cgwnsb(I)=(CONSQP1-CONSQ)/(DEPB(I+1)-DEPB(I))
      cwnsb(I)=CONSQ-cgwnsb(I)*DEPB(I)
C
      CONSQ=CONSQP1
C
  100 CONTINUE
C
C     THE GRADIENT IN THE LAST LAYER (NPBM1) WILL BE USED TO 
C     EXTRAPOLATE VALUES BELOW THE LAST POINT ON THE PROFILE
C
      cgwnsb(NPB)=cgwnsb(NPBM1)
      cwnsb(NPB)=CONSQ-cgwnsb(NPB)*DEPB(NPB)
C
      RETURN
      END
